<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('portal', $this->session->userdata['user_lang']);
		$this->lang->load('login', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$page_data = array();
		$data = $_POST;
		
		if (isset($data['email']))
		{
			$this->load->model('Membership_model', 'member');
			$member_data = $this->member->get($data);
		
			if (!$member_data)
			{
				$page_data['warning_error'] = '<h2>'.$this->lang->line('login_erroruser1').'</h2>
				<p>'.$this->lang->line('login_erroruser2').' <a href="'.base_url().'help/browser">'.$this->lang->line('login_erroruser3').'</a>.</p>
				<p>'.$this->lang->line('login_erroruser4').'</p>';
			}
			else
			{
				$this->load->helper('password');
				
				if ($member_data->password == encrypt_password($data['pass']))
				{
					$this->load->helper('cookie');
					delete_cookie('persistent');
					$cookie_pass = encrypt_cookie($member_data->password);
					
					$session_data = array('idmember' => $member_data->id, 'pass' => $cookie_pass, 'idtype' => $member_data->idtype);			
					
					if (isset($data['persistent']))
					{
						set_cookie('persistent', serialize($session_data), time() + 86400);
					}
					
					$this->load->model('Membership_login_model', 'member_login');
					$this->member_login->insert(array('idmember' => $member_data->id, 'cookie' => $member_data->password));
					$this->load_session($session_data);
				}
				else
				{
					$page_data['warning_error'] = '<h2>'.$this->lang->line('login_errorpass1').'</h2>
					<p>'.$this->lang->line('login_errorpass2').'</p>
					<p>'.$this->lang->line('login_errorpass3').' <a href="'.base_url().'recover">'.$this->lang->line('login_errorpass4').'</a></p>';
				}
			}
		}
		
		$page_data['main_content'] = 'login/index';
		$page_data['page_title'] = $this->lang->line('login_title');
		$page_data['header_base'] = '<a href="'.base_url().'register" class="button"><span>'.$this->lang->line('login_registerbutton').'</span></a>
		<span class="highlighttext">'.$this->lang->line('login_registertext').'</span>';
		$this->load->view('template-portal', $page_data);
	}
}

?>